package com.android.scanner.fragment;

import java.util.ArrayList;
import java.util.List;

import com.android.scanner.R;
import com.android.scanner.adapter.CommonAdapter;
import com.android.scanner.adapter.SelectAdapter;
import com.android.scanner.utils.Constant;
import com.android.scanner.utils.PreferencesUtil;

import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.Button;
import android.widget.ListView;

public class OrderFragment extends BaseFragment implements OnClickListener,
		OnItemSelectedListener, OnItemClickListener {
	private Handler handler;
	private Context context;
	private List<String> list = null;
	private SelectAdapter adapter = null;
	private ListView lv;
	private Button btnLeft, btnRight;
	private int curPosition = 0;
	private int[] orders = { R.string.orderbytime, R.string.orderbyabc,
			R.string.orderbylg };
	private static OrderFragment mSelft = null;

	public static OrderFragment getInstance(Handler handler) {
		if (mSelft != null)
			return mSelft;
		return new OrderFragment(handler);
	}

	public OrderFragment(Handler handler) {
		this.handler = handler;
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		context = getActivity();
		super.onCreate(savedInstanceState);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		View view = inflater.inflate(R.layout.fragment_order, null);
		initViews(view);
		return view;
	}

	@Override
	public void onResume() {
		// TODO Auto-generated method stub
		// if (adapter != null) {
		// Log.e("kirk", "kirk add - curPosition is " + curPosition);
		// lv.setSelection(curPosition);
		// adapter.updateHLPosition(curPosition);
		// }
		super.onResume();
	}

	@Override
	public void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
	}

	private void initViews(View view) {
		// TODO Auto-generated method stub

		btnLeft = (Button) view.findViewById(R.id.btn_left);
		btnRight = (Button) view.findViewById(R.id.btn_right);
		btnLeft.setOnClickListener(this);
		btnRight.setOnClickListener(this);

		list = new ArrayList<String>();

		for (int i = 0; i < orders.length; i++)
			list.add(context.getResources().getString(orders[i]));
		lv = (ListView) view.findViewById(R.id.lv);

		adapter = new SelectAdapter(context, list, lv, curPosition);
		lv.setAdapter(adapter);
		lv.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
		lv.requestFocus();
		lv.setSelection(curPosition);

		lv.setOnItemSelectedListener(this);
		lv.setOnItemClickListener(this);

		int order = PreferencesUtil.getIntPref(context,
				PreferencesUtil.NAME_ORDER, 0);
		lv.setItemChecked(order, true);
	}

	@Override
	public void onClick(View arg0) {
		// TODO Auto-generated method stub
		if (arg0.getId() == R.id.btn_left) {
			setCheckedItem();
			handler.sendEmptyMessage(Constant.MSG_BACK_OPTION);
		} else if (arg0.getId() == R.id.btn_right) {
			handler.sendEmptyMessage(Constant.MSG_BACK_OPTION);
		}
	}

	@Override
	public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
			long arg3) {
		// TODO Auto-generated method stub
		curPosition = arg2;
		adapter.updateHLPosition(curPosition);
	}

	@Override
	public void onNothingSelected(AdapterView<?> arg0) {
		// TODO Auto-generated method stub
	}

	@Override
	public boolean setKeyEvent(int keyCode, KeyEvent event) {
		// TODO Auto-generated method stub
		// Toast.makeText(context, "key Event is " + keyCode,
		// Toast.LENGTH_SHORT)
		// .show();
		if (keyCode == KeyEvent.KEYCODE_MENU) {
			btnLeft.callOnClick();
		} else if (keyCode == KeyEvent.KEYCODE_BACK) {
			btnRight.callOnClick();
		} else if (keyCode == KeyEvent.KEYCODE_F5) {
		} else if (keyCode == KeyEvent.KEYCODE_DPAD_CENTER) {
			setCheckedItem();
			return true;
		}
		return super.setKeyEvent(keyCode, event);
	}

	public void setCheckedItem() {
		for (int i = 0; i < list.size(); i++) {
			lv.setItemChecked(i, false);
		}
		lv.setItemChecked(curPosition, true);
		PreferencesUtil.setIntPref(context, PreferencesUtil.NAME_ORDER,
				curPosition);
	}

	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
		// TODO Auto-generated method stub
		setCheckedItem();
	}

}
